package com.tk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tk.util.*;

public class DeleteContent extends HttpServlet {

	public DeleteContent() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request,response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		
		
		int id = Integer.parseInt(request.getParameter("id"));
		int isTheme = Integer.parseInt(request.getParameter("isTheme"));
		int theme = Integer.parseInt(request.getParameter("theme"));
		
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps1 = null;
		PreparedStatement ps2 = null;
		String sql1 = "delete from post where id=?";
		String sql2 = "delete from post where pid=?";
			
		
		try {
			ps1 = conn.prepareStatement(sql1);
			ps1.setInt(1, id);
			ps1.execute();
			if( isTheme==1 ){
				ps2 = conn.prepareStatement(sql2);
				ps2.setInt(1, theme);
				ps2.execute();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if( isTheme==1 ){
				DBUtil.close(ps2);
			}
			if( ps1!=null ){
				DBUtil.close(ps1);
			}
			DBUtil.close(conn);

		}
		
		request.getRequestDispatcher("PaginationTheme").forward(request, response);
		
		/*if( isTheme==1 ){
			request.getRequestDispatcher("DisplayTheme").forward(request, response);
		}else{
			request.getRequestDispatcher("DisplayContent").forward(request, response);
		}*/	
		
	}

}
